package org.lynn.lambda.test;

import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

/**
 * java8 中新增的 集合方法
 *
 * @author linzou@linkdoc.com
 * @ClassName: JCFWithLambda
 * @Description:
 * @date 2019/6/27 11:37 AM
 */
public class JCFListWithLambda {

    /**
     * demo 数据
     */
    List<String> list = new ArrayList<>(Arrays.asList("I", "love", "you", "too"));

    @Test
    public void foreachWithJava7() {
        for (String s : list) {
            if (s.length() > 3) {
                System.out.println(s);
            }
        }
    }


    @Test
    public void foreachWithAnonymousClass() {

        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                if (s.length() > 3) {
                    System.out.println(s);
                }
            }
        });
    }

    @Test
    public void foreachWithLambda() {

        list.forEach(s -> {
            if (s.length() > 3) {
                System.out.println(s);
            }
        });

    }

    @Test
    public void removeIfWithJava7() {

        Iterator<String> iterator = list.iterator();

        while (iterator.hasNext()) {
            if (iterator.next().length() > 3) {
                iterator.remove();
            }
        }

        System.out.println(list);

    }

    @Test
    public void removeIfWithAnonymousClass() {

        list.removeIf(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length() > 3;
            }
        });

        System.out.println(list);

    }


    @Test
    public void removeIfWithLambda() {
        list.removeIf(str -> str.length() > 3);
        System.out.println(list);
    }


    @Test
    public void replaceAllWithJava7() {

        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (s.length() > 3) {
                list.set(i, s.toUpperCase());
            }
        }

        System.out.println(list);
    }

    @Test
    public void replaceAllWithAnonymousClass() {

        list.replaceAll(new UnaryOperator<String>() {
            @Override
            public String apply(String s) {
                if (s.length() > 3) {
                    return s.toUpperCase();
                }

                return s;
            }
        });

        System.out.println(list);

    }


    @Test
    public void replaceAllWithLambda() {

        list.replaceAll(str -> {
            if (str.length() > 3) {
                return str.toUpperCase();
            }
            return str;
        });

        System.out.println(list);

    }


    @Test
    public void sortWithJava7() {
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.length() - s2.length();
            }
        });
        System.out.println(list);

    }


    @Test
    public void sortWithLambda() {
        list.sort((s1, s2) -> s1.length() - s2.length());
        System.out.println(list);
    }


    @Test
    public void sortWithComparator() {
        list.sort(Comparator.comparingInt(String::length));
        System.out.println(list);
    }


}
